package com.neuedu.funlifebbs.controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.neuedu.funlifebbs.bean.Topics;
import com.neuedu.funlifebbs.query.TopicsQuery;
import com.neuedu.funlifebbs.service.TopicsServic;
import com.neuedu.funlifebbs.utils.JsonModel;

/**
 * @author jds
 *
 * @version 2019年8月19日 上午10:12:43
 */
@Controller
@RequestMapping("admin/topics")
public class TopicsController {

	@Autowired
	TopicsServic topicsServic;

	/**
	 * 帖子列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/listUI", method = RequestMethod.GET)
	public String list() {
		return "back/topics/list";

	}

	/**
	 * 帖子分页查询
	 * 
	 * @param topicsQuery
	 * @return
	 * @throws IOException
	 */

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public JsonModel list(TopicsQuery topicsQuery) throws IOException {
		PageInfo<Topics> pageInfo = topicsServic.query(topicsQuery);
		JsonModel jsonModel = new JsonModel();
		jsonModel.setMsg("查找帖子成功");
		jsonModel.setSuccess(true);
		jsonModel.setTotal(pageInfo.getTotal());
		jsonModel.setRows(pageInfo.getList());
		return jsonModel;
	}

	@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
	public String editUi(Model model, @PathVariable int id) {
		//
		Topics topics = topicsServic.findById(id);
		model.addAttribute("topics", topics);
		return "back/topics/edit";
	}

	@RequestMapping(value = "/save", method = RequestMethod.GET)
	public String saveUI() {
		return "back/topics/save";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String save(Topics topics) {

		if (topics.getId() != null) {
			topicsServic.update(topics);
		} else {
			topicsServic.add(topics);
		}
		return "redirect:listUI";
	}

}
